package cn.com.nxt.txb.httpdelegate;

import android.util.Log;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import cn.com.nxt.txb.HttpProtocol;
import cn.com.nxt.txb.ResponseStatus;
import cn.com.nxt.txb.model.BaseModel;
import cn.com.nxt.txb.util.AppConstant;

/**
 * Created by zhangling on 2017/5/30.
 * 拨号相关
 */

public class DialingDelegate {
    private HttpProtocol protocol;

    public DialingDelegate(HttpProtocol protocol) {
        this.protocol = protocol;
    }

    public void getOtherName(String token,String remotePhone, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.GET_OTHER_NAME);
        rp.setAsJsonContent(true);
        rp.addQueryStringParameter("token", token);
        rp.addQueryStringParameter("remote_phone", remotePhone);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--getOtherName_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--getOtherName_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void callRecord(String param,String token, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.CALL_RECORD);
        rp.setAsJsonContent(true);
        rp.setHeader("token", token);
        rp.setBodyContent(param);
        x.http().post(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--call_record_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else if (code.equals("10001")) {
                    protocol.failure("用户名存在", flag, ResponseStatus.REQUEST_FAILURE);
                } else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--call_record_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void getCallRecord(String token, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.GET_CALL_RECORDS);
        rp.setAsJsonContent(true);
        rp.addQueryStringParameter("token", token);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--get_record_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--get_record_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }

    public void getPersons(String token, final byte flag) {
        RequestParams rp = new RequestParams(AppConstant.GET_RECENT_PERSONS);
        rp.setAsJsonContent(true);
        // rp.setHeader("Content-type", "application/json");
        rp.addQueryStringParameter("token", token);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // Gson gson = new Gson();
                Log.v("--get_persons_success--", result);
                BaseModel model = BaseModel.parseResult(result);
                String code = model.getCode();
                if (code.equals("10000"))
                    protocol.success(model.getData(), flag);
                else
                    protocol.failure("请求失败，请重试", flag, ResponseStatus.REQUEST_FAILURE);
            }

            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("--get_persons_error--", ex.getMessage());
                protocol.failure("网络连接失败，请稍后重试", flag, ResponseStatus.NET_ERROR);
            }

            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                protocol.cancelled(flag);
            }

            @Override
            public void onFinished() {
                protocol.finished(flag);
            }
        });
    }
}
